﻿@using System.Diagnostics;
@using System.Collections;
@using System.Reflection;
@using System.Runtime.Versioning;
@using NewLife.Common;
@{
    Layout = NewLife.Cube.Setting.Current.Layout;

    ViewBag.Title = "服务器信息";
    var Act = ViewBag.Act;

    var asm = Assembly.GetExecutingAssembly();
    var att = asm.GetCustomAttribute<TargetFrameworkAttribute>();
    var ver = att.FrameworkDisplayName ?? att.FrameworkName;
}
@if (String.IsNullOrEmpty(Act))
{
    <table class="table table-bordered table-hover table-striped table-condensed">
        <thead>
            <tr>
                <th colspan="4">
                    服务器信息
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="name">
                    应用系统：
                </td>
                <td class="value">
                    @HttpRuntime.AppDomainAppVirtualPath&nbsp;<a href="@Url.Action("Main/Restart")" onclick="return confirm('仅重启ASP.Net应用程序域，而不是操作系统！\n确认重启？')">重启应用系统</a>
                </td>
                <td class="name">
                    目录：
                </td>
                <td class="value">
                    @HttpRuntime.AppDomainAppPath
                </td>
            </tr>
            <tr>
                <td class="name">
                    域名地址：
                </td>
                <td class="value">
                    @Request.ServerVariables["SERVER_NAME"]，
                    @Request.ServerVariables["LOCAl_ADDR"]:@Request.ServerVariables["Server_Port"]
                    &nbsp;[@Request.ServerVariables["REMOTE_HOST"]]
                    <a href="@Url.Action("Main/ServerVar")" target="_blank" title="点击打开服务器变量列表">服务器变量列表</a>
                </td>
                <td class="name">
                    计算机用户：
                </td>
                <td class="value">
                    @Environment.UserName/@Environment.MachineName
                </td>
            </tr>
            <tr>
                <td class="name">
                    应用程序域：
                </td>
                <td class="value">
                    @AppDomain.CurrentDomain.FriendlyName
                    <a href="@Url.Action("Main/Assembly")" target="_blank" title="点击打开进程程序集列表">程序集列表</a>
                </td>
                <td class="name">
                    .Net 版本：
                </td>
                <td class="value">
                    @Environment.Version &nbsp;@ver
                </td>
            </tr>
            <tr>
                <td class="name">
                    操作系统：
                </td>
                <td class="value">
                    @Runtime.OSName
                </td>
                <td class="name">
                    Web服务器：
                </td>
                <td class="value">
                    @ViewBag.WebServerName
                </td>
            </tr>
            <tr>
                <td class="name">
                    处理器：
                </td>
                <td class="value">
                    @Environment.ProcessorCount
                    核心
                </td>
                <td class="name">
                    时间：
                </td>
                <td class="value" title="这里使用了服务器默认的时间格式！后面是开机时间。">
                    @DateTime.Now，开机@(new TimeSpan(0, 0, 0, 0, Environment.TickCount))
                </td>
            </tr>
            <tr>
                @{ var process = Process.GetCurrentProcess();}
                <td class="name">
                    内存：
                </td>
                <td class="value">
                    物理内存：@(Runtime.AvailableMemory.ToString("n0"))M / @(Runtime.PhysicalMemory.ToString("n0"))M
                    工作集: @((process.WorkingSet64 / 1024 / 1024).ToString("n0"))M
                    提交: @((process.PrivateMemorySize64 / 1024 / 1024).ToString("n0"))M
                    GC: @((GC.GetTotalMemory(false) / 1024 / 1024).ToString("n0"))M
                    <a href="@Url.Action("Main/ProcessModules")" target="_blank" title="点击打开进程模块列表">模块列表</a>
                </td>
                <td class="name">
                    进程时间：
                </td>
                <td class="value">
                    @process.TotalProcessorTime.TotalSeconds.ToString("N2")秒 启动于 @process.StartTime.ToFullString()
                </td>
            </tr>
            <tr>
                <td class="name">
                    Session：
                </td>
                <td class="value">
                    @Session.Contents.Count 个，@Session.Timeout 分钟，SessionID：@Session.Contents.SessionID
                    <a href="@Url.Action("Main/Session")" target="_blank" title="点击打开Session列表">Session列表</a>
                </td>
                <td class="name">
                    Cache：
                </td>
                <td class="value">
                    @Cache.Count 个，可用：@((Cache.EffectivePrivateBytesLimit / 1024).ToString("n0"))KB
                    <a href="@Url.Action("Main/Cache")" target="_blank" title="点击打开Cache列表">Cache列表</a>
                </td>
            </tr>
        </tbody>
    </table>
    <table class="table table-bordered table-hover table-striped table-condensed">
        <thead>
            <tr>
                <th>名称</th>
                <th>标题</th>
                <th>文件版本</th>
                <th>内部版本</th>
                <th>编译时间</th>
                <th>描述</th>
            </tr>
        </thead>
        <tbody>
            @foreach (AssemblyX item in ViewBag.MyAsms)
            {
                <tr>
                    <td>@item.Name</td>
                    <td>@item.Title</td>
                    <td>@item.FileVersion</td>
                    <td>@item.Version</td>
                    <td>@item.Compile.ToFullString()</td>
                    <td>@item.Description</td>
                </tr>
            }
        </tbody>
    </table>
                    }
@if (Act == "ProcessModules")
{
    var isAll = String.Equals("All", Request["Mode"], StringComparison.OrdinalIgnoreCase);

    var process = Process.GetCurrentProcess();
    var list = new List<ProcessModule>();
    foreach (ProcessModule item in process.Modules)
    {
        if (isAll || item.FileVersionInfo.CompanyName != "Microsoft Corporation") { list.Add(item); }
    }

    <table class="table table-bordered table-hover table-striped table-condensed">
        <tr>
            <th colspan="7">
                进程模块(@process.ProcessName , PID=@process.Id )
                @if (!isAll)
                {
                    <text>（<a href="?Mode=All">完整</a>，仅用户）：</text>
                }
                else
                {
                    <text>（完整，<a href="?Mode=OnlyUser">仅用户</a>）：</text>
                }
            </th>
        </tr>
        <tr>
            <th>
                模块名称
            </th>
            <th>
                公司名称
            </th>
            <th>
                产品名称
            </th>
            <th>
                描述
            </th>
            <th>
                版本
            </th>
            <th>
                大小
            </th>
            <th>
                路径
            </th>
        </tr>
        @foreach (ProcessModule item in list)
        {
            <tr>
                <td>
                    @item.ModuleName
                </td>
                <td>
                    @item.FileVersionInfo.CompanyName
                </td>
                <td>
                    @item.FileVersionInfo.ProductName
                </td>
                <td>
                    @item.FileVersionInfo.FileDescription
                </td>
                <td>
                    @item.FileVersionInfo.FileVersion
                </td>
                <td>
                    @item.ModuleMemorySize
                </td>
                <td>
                    @item.FileName
                </td>
            </tr>
        }
    </table>
}
@if (Act == "Assembly")
{
    <table class="table table-bordered table-hover table-striped table-condensed">
        <tr>
            <th colspan="6">
                程序集列表(@AppDomain.CurrentDomain.FriendlyName )
            </th>
        </tr>
        <tr>
            <th>名称</th>
            <th>标题</th>
            <th>文件版本</th>
            <th>内部版本</th>
            <th>编译时间</th>
            <th>路径</th>
        </tr>
        @foreach (AssemblyX item in ViewBag.Asms)
        {
            <tr>
                <td>
                    @item.Name
                </td>
                <td>
                    @item.Title
                </td>
                <td>
                    @item.FileVersion
                </td>
                <td>@item.Version</td>
                <td>@item.Compile.ToFullString()</td>
                <td>
                    @{
                        var location = String.Empty;
                        try
                        {
                            location = item.Asm.Location;
                        }
                        catch { }
                    }
                    @location
                </td>
            </tr>
                        }
    </table>
                        }
@if (Act == "Session")
{
    <table class="table table-bordered table-hover table-striped table-condensed">
        <tr>
            <th colspan="6">
                Session列表(@Session.Contents.Count )
            </th>
        </tr>
        <tr>
            <th>名称</th>
            <th>类型</th>
            <th>数值</th>
        </tr>
        @foreach (String item in Session.Keys)
        {
            <tr>
                <td>@item</td>
                <td>@Session[item].GetType().FullName</td>
                <td>@Session[item]</td>
            </tr>
        }
    </table>
}
@if (Act == "Cache")
{
    <table class="table table-bordered table-hover table-striped table-condensed">
        <tr>
            <th colspan="6">
                Cache列表(@Cache.Count )
            </th>
        </tr>
        <tr>
            <th>名称</th>
            <th>类型</th>
            <th>数值</th>
        </tr>
        @foreach (DictionaryEntry item in Cache)
        {
            <tr>
                <td>@item.Key</td>
                <td>@item.Value.GetType().FullName</td>
                <td>@item.Value</td>
            </tr>
        }
    </table>
}
@if (Act == "ServerVar")
{
    <table class="table table-bordered table-hover table-striped table-condensed">
        <tr>
            <th colspan="6">
                服务器变量列表
            </th>
        </tr>
        <tr>
            <th>名称</th>
            <th>数值</th>
        </tr>
        @foreach (var key in Request.ServerVariables.AllKeys)
        {
            var v = Request.ServerVariables[key];
            v = v.Replace("\r\n", "</br>");
            if (key.EqualIgnoreCase("HTTP_COOKIE")) { v = v.Replace(";", "</br>"); }
            <tr>
                <td>@key</td>
                <td>@Html.Raw(v)</td>
            </tr>
        }
    </table>
}
